var app = angular.module("detailsApp", []); 
var ws,uid,type,collection_id,attention_id;//id,
app.controller("detailsController", function($scope, $http,$sce) {
	$scope.huiContent_id = 0;
	$scope.master = 0;//发布人的iD
	$scope.initView = function() {
		plus.nativeUI.showWaiting('加载中...');
		$scope.self_id = uid;//获取登录用户的ID    判断是否在社区和交易里面是否显示	
		$http({ 
			method: 'post',
			url: apiRoot,
			data: {
				action: 'Community.details',
				id:collection_id,
				uid:uid
			}
		}).then(function successCallback(response) {
			var info = response.data.data;	
			$scope.master = info.note.uid; 
			$scope.huanxinId = info.note.mobile;  
			$scope.huanxinIcon = info.note.icon;
			$scope.huanxinNick= info.note.nick;
			$scope.nid = info.note.id;
			$scope.note = info.note; //标题下的数据  
			$scope.goods = info.goods;
			$scope.comments = info.comment; 
			$scope.user = info.user; 
			$scope.videoUrl = $sce.trustAsResourceUrl(info.note.subject); 
			$scope.content_imgs = info.note.content_imgs; 
			plus.nativeUI.closeWaiting(); 
		}, function errorfunction(e) {
			plus.nativeUI.closeWaiting(); 
			console.log(JSON.stringify(e));
		})
		
		//判断是否是关注的状态	
		$http({
			method:'post',  
			url:apiRoot,
			data:{
				action:'Community.checkFollowStatus',
				uid:uid,
				type:2,			
				type_id:attention_id
			}
		}).then(function successCallback(response){
			plus.nativeUI.closeWaiting();
			if(response.data.error == 121){
				$scope.attention = 121;
			}else{
				$scope.attention = 115;/*显示取消关注*/
			}		
		},function errorfunction(e){ 
			console.log(JSON.stringify(e));
		})
	
		//判断是否是收藏的状态	
		$http({
			method:'post',
			url:apiRoot,
			data:{
				action:'Community.checkCollectStatus',
				uid:uid,
				type:0,
				type_id:collection_id
			}
		}).then(function successCallback(response){
			if(response.data.error == 121){
				$scope.collection = 121;
			}else{
				$scope.collection = 115;/*显示取消收藏*/
			}	
			$scope.already = 1;
		},function errorfunction(e){ 
			console.log(JSON.stringify(e));
		})
	}
	
	/*关注/取消关注*/
	var is_clickAttention = true;
	$scope.isattention = function(way){	
		if(is_clickAttention){
		$http({
			method:'post',
			url:apiRoot,
			data:{
				action:'Community.follow',
				uid:uid,
				type:2,				
				type_id:attention_id
			}
		}).then(function successCallback(response){
			if(response.data.error == 115){
				/*操作成功*/
				if(way == 2){
					$scope.attention = 121;/*显示关注*/
					toast("已取消关注");
				}else{
					$scope.attention = 115;/*显示取消关注*/
					toast("已关注");
				}				
			}
		},function errorfunction(e){ 
			errortoast();
			console.log(JSON.stringify(e));
		})	
		is_clickAttention = false;
		setTimeout(function(){
			is_clickAttention = true;
		},2000)
		}		
	}
	
	/*收藏/取消收藏*/
	var is_clickCollection = true;
	$scope.iscollection = function(way){
		if(is_clickCollection){
		plus.nativeUI.showWaiting('');
		$http({
			method:'post',
			url:apiRoot,
			data:{
				action:'Community.collection',
				uid:uid,
				type:0,
				type_id:collection_id
			}
		}).then(function successCallback(response){
			console.log(JSON.stringify(response));
			if(response.data.error == 115){
				/*操作成功*/
				if(way == 1){
					$scope.collection = 121;/*显示关注*/
					toast("已取消收藏");
				}else{
					$scope.collection = 115;/*显示取消关注*/
					toast("已收藏");
				}				
			}
			plus.nativeUI.closeWaiting();
			plus.storage.setItem('refresh','cur_refresh');//传值到列表页，刷新页面
		},function errorfunction(e){ 
			errortoast();
			console.log(JSON.stringify(e));
		})
		is_clickCollection = false;
		setTimeout(function(){
			is_clickCollection = true;
		},2000)
		}
	}
	
	//groupList
	$scope.groupList = function(){
		openPage("chose_group.html",{master_id:$scope.master});
	}
	//私聊
	$scope.openChat = function(){
		window.plus.bridge.exec("community", "sendMessage", [$scope.huanxinId+'',$scope.huanxinIcon,$scope.huanxinNick]);
	}
	//商品详情
	$scope.goodsInfo = function(uid,type,id) {
		plus.webview.create('goods_detail.html', 'goods_detail.html',{},{gUid:uid,type:type,gId:id}).show('pop-in');
	}
	//进入个人主页
	$scope.icon_sub =function(id,img,nick,type_id){
		plus.webview.create('honer_page.html', 'honer_page.html',{},{icon_id:id,icon_img:img,icon_name:nick,type_id:type_id}).show('pop-in');
	}
	
	//跳转到举报页面
	$scope.report = function (uid,type) {
		console.log(uid+"#####"+type);
		plus.webview.create('report.html','report.html',{},{reportUid:uid,reportType:type}).show('pop-in');/*更多商品*/
	}
	
	$scope.viewContent = function() {
		//parent 
		viewval = $('.conmentval').val();
		if(viewval == ""){
			mui.toast('您发布的内容不能为空');
			return;
		}
		$http({
			method: 'post',
			url: apiRoot,
			data: {
				action: 'Community.content',
				id: $scope.nid,
				uid: uid,
				content: viewval,
				parent: $scope.huiContent_id,
			}
		}).then(function successCallback(response) {
			console.log(JSON.stringify(response));
			$scope.initView();
			toast('发表评论成功'); 
//			$scope.show_dialog = 1; //显示对话框 
		}, function errorfunction(e) {
			console.log(JSON.stringify(e));
		})
	}
	$scope.huiContent = function(pid) {
		$scope.huiContent_id = pid;
		$('.input_box').animate({
			left: '0px'
		}, 300);
//		return false;
	}
	
})



document.addEventListener("plusready", function() {
	ws =plus.webview.currentWebview();
	my = plus.webview.currentWebview().my;
	appElement = document.querySelector('[ng-controller=detailsController]');
	$scope = angular.element(appElement).scope();
	$('input_box input').on('blur',function(){
		if(document.activeElement.tagName == 'INPUT'){
			$(this).css({'position':'absolute',"top":$(window).scrollTop()+$(window).height()})
		}else{
			$(this).css({'position':'fixed',"bottom":0})
		}
	})
	//取值
	uid = plus.storage.getItem('uid');//获取登录账号人的id( 自己的id )
	attention_id = ws.noteUid;//关注人的id
	collection_id = ws.noteId;   //收藏内容的ID
	if(my == 'my'){
		$('.borderNone').css('display','none');
	}
	$scope.topic=ws.topic;  //专题
	$scope.initView();//视图显示
	type = ws.type;/*类型*/
	$scope.$apply();
}) 